//
//  ScoreView.m
//  intelligent
//
//  Created by Ace Portgas on 2024/3/26.
//

#import "ScoreView.h"
#import "StarView.h"
@interface ScoreView()<StarViewDelegate>
/**
 *  背景蒙版
 */
@property (weak,nonatomic) UIButton * backButton;
/**
 *  简介文字
 */
@property (strong,nonatomic) UILabel * titleLabel;
/**
 *  描述文本
 */
@property (strong,nonatomic) UILabel * descriptionLabel;
/**
 *  星星视图
 */
@property (strong,nonatomic) StarView * starView;
/**
 *  确认按钮
 */
@property (strong,nonatomic) UIButton * confirmButton;
/**
 *  吐槽按钮按钮
 */
@property (strong,nonatomic) UIButton * feedbackButton;
/**
 *  评分
 */
@property (assign,nonatomic) NSInteger selectScore;

@end
@implementation ScoreView

//是否弹评价弹窗
+(BOOL)SQ_ShowScoreView{
    //判断是否是第-次发言
    NSString * homeFirstQuestion = [Manager SQ_stringForKey:@"homeFirstQuestion"];
    if ([homeFirstQuestion isNotEmpty]) {
        
        NSString * yipingjia = [Manager SQ_stringForKey:@"yipingjia"];
        if ([yipingjia isNotEmpty]) {
             return NO;
        }
        return  YES;
    } else {
        [Manager SQ_SetObject:@"2" FormKey:@"homeFirstQuestion"];
        return  NO;
    }
}

//是否弹评价弹窗
+(BOOL)SQ_ShowScoreViewWithClass:(NSString *)className{
    //判断是否是第-次发言
    NSString * homeFirstQuestion = [Manager SQ_stringForKey:className];
    if ([homeFirstQuestion isNotEmpty]) {
        
        NSString * yipingjia = [Manager SQ_stringForKey:@"yipingjia"];
        if ([yipingjia isNotEmpty]) {
             return NO;
        }
        return  YES;
    } else {
        [Manager SQ_SetObject:@"2" FormKey:className];
        return NO;
    }
}


-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self){
        _selectScore = 5;
        
        UIButton * backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        backButton.backgroundColor = [UIColor colorWithHexString:@"#000000" alpha:0.4];
        backButton.frame = frame;
        [backButton addTarget:self action:@selector(clickHideView) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:backButton];
        self.backButton = backButton;
        
        CGFloat viewW = 320;
        CGFloat viewH = 420;
        CGFloat viewX = (SCREEN_WIDETH - viewW) / 2;
        CGFloat viewY = (SCREEN_HEIGHT - viewH) / 2;
        UIView * view = [[UIView alloc]initWithFrame:CGRectMake(viewX, viewY, viewW, viewH)];
        view.backgroundColor = [UIColor colorWithHexString:@"#ffffff"];
        view.layer.cornerRadius = 16;
        [self addSubview:view];
        
        CGFloat scoreImageW = 310;
        CGFloat scoreImageH = 92;
        CGFloat scoreImageX = (viewW - scoreImageW) / 2;
        CGFloat scoreImageY = 5;
        _scoreImage = [[UIImageView alloc]init];
        _scoreImage.image = SQGetImage(@"编组 4");
        _scoreImage.frame = CGRectMake(scoreImageX, scoreImageY, scoreImageW, scoreImageH);
        [view addSubview:_scoreImage];
        
        CGFloat scoreW = 238;
        CGFloat scoreH = 34;
        CGFloat scoreX = (viewW - scoreW) / 2;
        CGFloat scoreY = scoreImageY + scoreImageH + 8;
        _starView = [[StarView alloc] initWithFrame:CGRectMake(scoreX, scoreY, scoreW, scoreH) numberOfStar:5];
        _starView.delegate = self;
        [view addSubview:_starView];
        
        CGFloat titleX = 20;
        CGFloat titleY = scoreY + scoreH + 23;
        CGFloat titleW = viewW - (titleX * 2);
        CGFloat titleH = 44;
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
        _titleLabel.textColor = [UIColor colorWithHexString:@"#333333"];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.numberOfLines = 0;
        _titleLabel.frame = CGRectMake(titleX, titleY, titleW, titleH);
        _titleLabel.text = SQNSLocalString(@"期待您的评价您的五星好评会让我们做得更好");
        [view addSubview:_titleLabel];
        
        _confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_confirmButton setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
        [_confirmButton setTitle:SQNSLocalString(@"立即评价") forState:UIControlStateNormal];
        [_confirmButton addTarget:self action:@selector(clickFeedbackScore) forControlEvents:UIControlEventTouchUpInside];
        _confirmButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
        [_confirmButton setBackgroundImage:SQGetImage(@"矩形备份") forState:UIControlStateNormal];
        [view addSubview:_confirmButton];
        [_confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.titleLabel.mas_bottom).mas_offset(40);
            make.left.mas_equalTo(view).mas_offset(30);
            make.right.mas_equalTo(view).mas_offset(-30);
            make.height.mas_offset(44);
        }];
        
        _feedbackButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_feedbackButton setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateNormal];
        [_feedbackButton setTitle:SQNSLocalString(@"吐槽一下") forState:UIControlStateNormal];
        [_feedbackButton addTarget:self action:@selector(clickFeedbackBtn) forControlEvents:UIControlEventTouchUpInside];
        _feedbackButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
        [_feedbackButton setBackgroundImage:SQGetImage(@"矩形 3") forState:UIControlStateNormal];
        [view addSubview:_feedbackButton];
        [_feedbackButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.confirmButton.mas_bottom).mas_offset(12);
            make.left.mas_equalTo(view).mas_offset(30);
            make.right.mas_equalTo(view).mas_offset(-30);
            make.height.mas_offset(44);
        }];
        
        UIButton * closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [closeBtn addTarget:self action:@selector(clickHideView) forControlEvents:UIControlEventTouchUpInside];
        [closeBtn setTitle:SQNSLocalString(@"残忍拒绝") forState:UIControlStateNormal];
        [closeBtn setTitleColor:[UIColor colorWithHexString:@"#999999"] forState:UIControlStateNormal];
        closeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [view addSubview:closeBtn];
        [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.feedbackButton.mas_bottom).mas_offset(12);
            make.left.mas_equalTo(view).mas_offset(30);
            make.right.mas_equalTo(view).mas_offset(-30);
            make.height.mas_offset(44);
        }];
    }
    return self;
}

-(void)clickFeedbackScore{
    [Manager SQ_SetObject:@"1111" FormKey:@"yipingjia"];
    if (_selectScore == 5) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(clickPushScore:)]){
            [self.delegate clickPushScore:_selectScore];
        }
    }else{
        
    }
    if (self.clickConfireBtnBlock) {
        self.clickConfireBtnBlock();
    }
//    [self clickHideView];
}

-(void)clickFeedbackBtn{
    if (self.clickFeedbackBtnBlock) {
        self.clickFeedbackBtnBlock();
    }
//    [self clickHideView];
}

-(void)starView:(StarView *)view score:(float)score{
    if (score > 0.9){
        _selectScore = 5;
    }else if (score > 0.7){
        _selectScore = 4;
    }else if (score > 0.5){
        _selectScore = 3;
    }else if (score > 0.3){
        _selectScore = 2;
    }else{
        _selectScore = 1;
    }
}

-(void)clickHideView{
    if (self.clickCancelBtnBlock) {
        self.clickCancelBtnBlock();
    }
//    [self removeFromSuperview];
}

@end
